import Vue from "vue"
import VueRouter from "vue-router"
import { environment } from "config/env.js"
// import { component } from "vue/types/umd";
// import routerModule from "router/routerModule/index.js";
Vue.use(VueRouter)

// 处理重复导航错误
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}
const routes = [
  {
    path: "/",
    name: "",
    component: (resolve) => require(["views/index/index.vue"], resolve),
    redirect: '/welcome',
    children: [
      {
        path: "/welcome",
        name: "welcome",
        component: (resolve) => require(["views/viewport/welcome/index.vue"], resolve),
        meta: {
          btnRule: [],
          breadcrumb: "欢迎页",
        },
      },
    ],
  },
  {
    path: "/login",
    name: "Login",
    component: (resolve) => require(["views/login/login.vue"], resolve),
    meta: {
      btnRule: [],
      breadcrumb: "登录",
      notRequiresAuth: true,
    },
  },
  {
    path: "/404",
    name: "notFound",
    component: (resolve) => require(["views/notFound/index.vue"], resolve),
    meta: {
      btnRule: [],
      breadcrumb: "notFound",
      notRequiresAuth: true,
    },
  },
  // ...routerModule,
]

const router = new VueRouter({
  base: "/resource_manage/admin/",
  mode: environment === "development" ? "hash" : "history",
  routes,
})
router.onError((error) => {
  const pattern = /Loading chunk (\d)+ failed/g
  const isChunkLoadFailed = error.message.match(pattern)
  console.log(isChunkLoadFailed, "isChunkLoadFailed")
  const targetPath = router.history.pending.fullPath
  if (isChunkLoadFailed) {
    router.replace(targetPath)
  }
})

export default router
